package com.jwaoo.chat;

import android.content.Context;
import com.jwaoo.chat.activity.P2PMessageActivity;
import com.jwaoo.chat.data.db.MessageHandler;
import com.jwaoo.chat.session.SessionEventListener;
import com.jwaoo.chat.session.SessionHelper;
import com.jwaoo.chat.session.emoji.StickerManager;
import com.jwaoo.im.SslContextBuilder;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
/**
 * Created by jwaoo on 2017/6/14.
 */

public class ChatHelpter {
    private static Context mContext;
    // 会话窗口消息列表一些点击事件的响应处理函数
    private static SessionEventListener sessionListener;
    // 单聊界面定制
    private static SessionCustomization commonP2PSessionCustomization;
    public static int currentSessionId = 0;
    private static List<String> activityList = new ArrayList<>();
    public static void init(Context context ,boolean isLogin,int id){
        mContext = context;
        SessionHelper.init();
        StickerManager.getInstance().init();
        if (!isLogin){
            initChat(id);
        }
    }


    public static void setActivityList(List<String> list){
        activityList = list;
    }

    public static boolean isActivityHasStarted(String name){
        return activityList.contains(name);
    }

    public static boolean isCurrentActivity(Class<?> clas){
        if (clas == null) return false;
        String className = clas.getSimpleName();
        if (activityList.isEmpty()) return false;
        if (!className.equals(activityList.get(activityList.size()-1))) return false;
        return true;
    }

    public static Context getContext(){
        return mContext;
    }

    /**
     * 设置单聊界面定制 SessionCustomization
     *
     * @param commonP2PSessionCustomization 聊天界面定制化
     */
    public static void setCommonP2PSessionCustomization(SessionCustomization commonP2PSessionCustomization) {
        ChatHelpter.commonP2PSessionCustomization = commonP2PSessionCustomization;
    }

    /**

     *
     * @param context 上下文
     * @param account 目标账号
     */
    public static void startP2PSession(Context context, int account) {
        ChatHelpter.startChatting(context, account, ChatConstants.P2P);
    }
    /**
     * 打开一个聊天窗口，开始聊天
     *
     * @param context       上下文
     * @param id            聊天对象ID（用户帐号account或者群组ID）
     * @param sessionType   会话类型
     */
    public static void startChatting(Context context, int id, int sessionType) {
        if (sessionType == ChatConstants.P2P) {
            P2PMessageActivity.start(context, id);
        }
    }
    /**
     * 获取聊天界面事件监听器
     *
     * @return
     */
    public static SessionEventListener getSessionListener() {
        return sessionListener;
    }

    /**
     * 设置聊天界面的事件监听器
     *
     * @param sessionListener
     */
    public static void setSessionListener(SessionEventListener sessionListener) {
        ChatHelpter.sessionListener = sessionListener;
    }

    public static void initChat(final int id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream inputStream = mContext.getAssets().open("config/cert.bks");
                    SslContextBuilder builder = new SslContextBuilder(inputStream);
                    SSLContext context = builder.build();
                    IMessageClient messageClient = new IMessageClient(id);
                    messageClient.setSslContext(context);
                    messageClient.setSslContext(context);
//                    messageClient.start("47.52.78.222", 18130);
                    messageClient.start("192.168.10.250", 8802);
                    MessageHandler.getInstace(mContext).setIMessageClient(messageClient);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }).start();

    }
}
